package com.yager.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.yager.model.House;
import com.yager.model.Order;
import com.yager.model.Page;
import com.yager.model.User;
import com.yager.service.HouseService;
import com.yager.service.OrderService;
import com.yager.service.UserService;
import com.yager.service.impl.UserServiceImpl;
import com.yager.util.DividePageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private HouseService houseService;

    @ResponseBody
    @RequestMapping("/addNewOrder")
    public void addNewOrder(HttpServletRequest req, HttpSession session){
        System.out.println("成功发送发布招募请求");
        User user=(User) session.getAttribute("user");
        House house=(House) session.getAttribute("Onehouse");
        String description=req.getParameter("orderdescription");
        orderService.publishNewOrder(user.getUserid(),house.getHouseid(),description);
    }

    @RequestMapping("/getHouseOrder")
    public String getHouseOrder(HttpServletRequest req, HttpSession session, @RequestParam(defaultValue = "1") Integer pageNo){
        House house=(House) session.getAttribute("Onehouse");

        if(!orderService.isHouseHasOrder(house.getHouseid())) return "order/noOrder";

        PageInfo<Order> orderPageInfo=orderService.getOrdersByHouseId(house.getHouseid(), pageNo);
        List<Order> orderList = orderPageInfo.getList();
        /*System.out.println("发送了一次查询合租信息请求，当前请求数量为："+ orderList);*/
//        System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+orderList.size());
        User user = userService.getUserById(orderList.get(0).getUserid());
        System.out.println("发布招募的user信息为："+user);
        session.setAttribute("orderuser", user);
        session.setAttribute("orderpage", orderPageInfo);
        return "order/showOrder";
    }

    @RequestMapping("/getUserOrders")
    public String getUserOrders(HttpSession session, @RequestParam(defaultValue = "1") Integer pageNo, Integer flag){
        User user=(User) session.getAttribute("user");
        if(!orderService.isUserHasOrder(user.getUserid())) return "order/noOrder";
        PageInfo<Order> orderpage=orderService.getOrdersByUserId(user.getUserid(),pageNo);
        List<Order> orderList=orderpage.getList();
        List<String> houseNameList=new ArrayList<>();

        for(Order o : orderList){
            houseNameList.add(houseService.getHouseNameById(o.getHouseid()));
        }
        session.setAttribute("orderpage",orderpage);
        session.setAttribute("orderhousename",houseNameList);
        System.out.println(flag);
        /*if(flag == null){
            return "order/myOrders";
        }else if(flag == 1){
            return "order/myOrders :: myOrdersInfo";
        }*/
        return "order/myOrders";
    }
    @RequestMapping("/removeOrder")
    public String removeOrder(HttpServletRequest req, String orderid){
      /*  String orderid=req.getParameter("orderid");*/
        System.out.println(orderid);
        orderService.deleteOrderById(orderid);
        return "/order/myOrders";
    }


/*    @RequestMapping("/toOrderPage")
    public String toOrderPage(HttpSession session,int pageNo){
        List<Order> list=(List<Order>) session.getAttribute("orderList");
        session.setAttribute("orderPage",page);
        return "#";
    }*/


     @RequestMapping("/toShowOrder")
     public String toShowOrder(){
         return "order/showOrder";
     }
    @RequestMapping("/toAddOrder")
    public String toAddOrder(){
        return "order/addOrder";
    }
}
